import 'package:easy_refresh/easy_refresh.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:sinosheets/components/flash_sale_product_item.dart';
import 'package:sinosheets/components/normal_product_item.dart';
import 'package:sinosheets/components/no_data_widget.dart';
import 'package:sinosheets/utils/theme/style_utils.dart';

import 'logic.dart';

class FavoritePage extends StatefulWidget {
  const FavoritePage({Key? key}) : super(key: key);

  @override
  State<FavoritePage> createState() => _FavoritePageState();
}

class _FavoritePageState extends State<FavoritePage> with TickerProviderStateMixin {
  final FavoriteLogic logic = Get.put(FavoriteLogic());
  late TabController _tabController;

  @override
  void initState() {
    super.initState();
    _tabController = TabController(length: 2, vsync: this);
    _tabController.addListener(() {
      logic.switchTab(_tabController.index);
    });
  }

  @override
  Widget build(BuildContext context) {
    return GetBuilder<FavoriteLogic>(builder: (controller) {
      return Scaffold(
        appBar: AppBar(
          title: Text('Favorite', style: TextStyle(fontSize: 22.r, fontWeight: FontWeight.bold, color: Colors.black)),
          elevation: 0,
          centerTitle: false,
          bottom: TabBar(
            controller: _tabController,
            indicatorColor: Color(0xFFFF7D00),
            labelColor: Color(0xFFFF7D00),
            unselectedLabelColor: ColorUtils.c_666666,
            tabs: [
              Tab(
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: [
                    Text('Flash Sale', style: TextStyle(fontSize: 16.r, fontWeight: FontWeight.bold)),
                    SizedBox(width: 4),
                    Container(
                      padding: EdgeInsets.symmetric(horizontal: 6, vertical: 2),
                      decoration: BoxDecoration(
                        color: Color(0xFFFF7D00),
                        borderRadius: BorderRadius.circular(10),
                      ),
                      child: Text(
                        '${controller.favoriteStats['flashSaleCount']}',
                        style: TextStyle(
                          color: Colors.white,
                          fontSize: 12,
                          fontWeight: FontWeight.bold,
                        ),
                      ),
                    ),
                  ],
                ),
              ),
              Tab(
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: [
                    Text('Normal', style: TextStyle(fontSize: 16.r, fontWeight: FontWeight.bold)),
                    SizedBox(width: 4),
                    Container(
                      padding: EdgeInsets.symmetric(horizontal: 6, vertical: 2),
                      decoration: BoxDecoration(
                        color: Color(0xFFFF7D00),
                        borderRadius: BorderRadius.circular(10),
                      ),
                      child: Text(
                        '${controller.favoriteStats['normalCount']}',
                        style: TextStyle(
                          color: Colors.white,
                          fontSize: 12,
                          fontWeight: FontWeight.bold,
                        ),
                      ),
                    ),
                  ],
                ),
              ),
            ],
          ),
        ),
        body: TabBarView(
          controller: _tabController,
          children: [
            // 限时抢购收藏
            _buildFlashSaleFavorites(controller),
            // 普通商品收藏
            _buildNormalFavorites(controller),
          ],
        ),
      );
    });
  }

  /// 构建限时抢购收藏列表
  Widget _buildFlashSaleFavorites(FavoriteLogic controller) {
    if (controller.flashSaleFavorites.isEmpty) {
      return NoDataWidget.emptyFavorites(
        title: 'No Flash Sale Favorites',
        description: 'You haven\'t added any flash sale items to your favorites yet.',
        showButton: false,
      );
    }

    return EasyRefresh(
      controller: controller.controller,
      onRefresh: controller.onRefresh,
      child: GridView.builder(
        padding: EdgeInsets.all(16.r),
        gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
          crossAxisCount: 2,
          childAspectRatio: 0.65,
          crossAxisSpacing: 12.r,
          mainAxisSpacing: 12.r,
        ),
        itemCount: controller.flashSaleFavorites.length,
        itemBuilder: (context, index) {
          final product = controller.flashSaleFavorites[index];
          return FlashSaleProductItem(item: product);
        },
      ),
    );
  }

  /// 构建普通商品收藏列表
  Widget _buildNormalFavorites(FavoriteLogic controller) {
    if (controller.normalFavorites.isEmpty) {
      return NoDataWidget.emptyFavorites(
        title: 'No Normal Favorites',
        description: 'You haven\'t added any normal items to your favorites yet.',
        showButton: false,
      );
    }

    return EasyRefresh(
      controller: controller.controller,
      onRefresh: controller.onRefresh,
      child: GridView.builder(
        padding: EdgeInsets.all(16.r),
        gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
          crossAxisCount: 2,
          childAspectRatio: 0.61,
          crossAxisSpacing: 12.r,
          mainAxisSpacing: 12.r,
        ),
        itemCount: controller.normalFavorites.length,
        itemBuilder: (context, index) {
          final product = controller.normalFavorites[index];
          return NormalProductItem(item: product);
        },
      ),
    );
  }

  @override
  void dispose() {
    _tabController.dispose();
    Get.delete<FavoriteLogic>();
    super.dispose();
  }
}
